package com.wechat.thread;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import com.wechat.model.Address;
import com.wechat.model.User;
import com.wechat.model.UserMessage;
import com.wechat.service.AddressService;
import com.wechat.service.UserMessageService;
@Service
public class MyThread implements ApplicationListener<ContextRefreshedEvent> {
	public static Set<User> userlist = new HashSet<User>();
	public static Set<UserMessage> userMessage = new HashSet<UserMessage>();
	public static Set<Address> address = new HashSet<Address>();
	private static int num = 0;
	@Autowired
	private UserMessageService userMessageService;
	@Autowired
	private AddressService addressService;
	/**
	 * 开启线程池
	 * 
	 * @dagewang 2017年11月24日
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent arg0) {
		if (num == 0) {
			ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(30, 60, 13000, TimeUnit.MILLISECONDS,
					new ArrayBlockingQueue<Runnable>(30), new ThreadPoolExecutor.AbortPolicy());
			threadPoolExecutor.execute(new SaveData(userMessageService, addressService));
		}
		num = num + 1;
	}

}
